from typing import List
import re


# class SentenceObj(object):
#     def __init__(self, sentence: str = None, label: List = None):
#         self.text = sentence
#         self.label = label if label is not None else []
#
#     def flash(self, sentence, label: List = None):
#         self.__init__(sentence, label)
#
#     def toJson(self):
#         if self.text is not None:
#             return json.dumps(self.__dict__)
#         else:
#             raise "SentenceObj 的输入内容不能为空"


class TextToList(object):
    def __init__(self, text: str, target_file: str = "./result.txt"):
        self.target_file = target_file
        self.result_lines = []
        self.text = text

    def getList(self) -> List[str]:
        self._textToJsonList()
        return self.result_lines

    def toFile(self, target_file: str = None):
        if len(self.result_lines) == 0:
            self.getList()

        if target_file is not None:
            self.target_file = target_file

        with open(self.target_file, "w+", encoding="utf-8") as result_file:
            for line in self.result_lines:
                result_file.write(str(line))
                result_file.write("\n")

    def _textToJsonList(self):
        new_text = re.sub("-\n+", "", self.text)
        new_text = new_text.replace("\n\n", ". ")
        new_text = new_text.replace("\n", " ")
        new_text = re.sub("[(][^)]+[)]", "", new_text)
        new_text = re.sub("[\d]+\.*[\d]*|[^\w ,.-]", "", new_text)
        new_text = re.sub("( ,)+", ", ", new_text)
        new_text = re.sub(" +", " ", new_text)
        new_text = re.sub("\.+ +", ".\r\n", new_text)

        # s_obj = SentenceObj()
        for line in new_text.splitlines():
            line = line.strip()
            if len(line) < 60 or not line[0].isupper():
                continue
            # result_lines.append(line.strip())
            # s_obj.flash(line.strip())
            self.result_lines.append(line.strip())
            # print(line.strip())
